# _*_ coding:UTF-8 _*_
"""
__author__ = 'shede333'
"""

import shell_command

_svn_username = None
_svn_password = None


def setup_info(user_name, password):
    global _svn_password
    global _svn_username
    _svn_username = user_name
    _svn_password = password


def _decorate_verification(svn_command):
    final_command = svn_command
    global _svn_password
    global _svn_username
    try:
        svn_option = " --non-interactive --no-auth-cache "
        final_command += svn_option
        if (_svn_username is not None) and (_svn_password is not None):
            name_pwd = " --username=%s --password=%s " % (_svn_username, _svn_password)
            final_command += name_pwd
    except Exception:
        print "decorate svn params error: %s" % svn_command
    finally:
        return final_command


def _fix_encode(command_str):
    return "export LANG=en_US.UTF-8; " + command_str


def _print_output(command, *info_list):
    print "*" * 15, "svn", "*" * 15
    print "command: "
    print "\t", command
    print "output:"
    print "\n".join(["\t" + tmp_info for tmp_info in info_list])
    print "*" * 30


def _run_command(command, is_check=False, print_log=True):
    run_code, run_output = shell_command.call(command, is_check=is_check)
    if print_log:
        _print_output(command, run_output)
    return run_code, run_output


def checkout(url, dst_dir_path, is_check=False, print_log=True):
    command = _decorate_verification("svn checkout")
    command = "{0} {1} '{2}'".format(command, url, dst_dir_path)
    return _run_command(command, is_check=is_check, print_log=print_log)


def add(svn_dir_path, file_path, print_log=True):
    if "@" in file_path:
        file_path += "@"
    command = "cd '{0}'; svn add '{1}'".format(svn_dir_path, file_path)
    return _run_command(command, print_log=print_log)


def add_files(svn_dir_path, file_path_list=(), print_log=True):
    command = "cd '{0}';".format(svn_dir_path)
    for file_path in file_path_list:
        if "@" in file_path:
            file_path += "@"
        command += "svn add '{0}';".format(file_path)
    return _run_command(command, print_log=print_log)


def delete(svn_dir_path, file_path, print_log=True):
    if "@" in file_path:
        file_path += "@"
    command = "cd '{0}'; svn delete '{1}'".format(svn_dir_path, file_path)
    return _run_command(command, print_log=print_log)


def update(svn_dir_path, print_log=True):
    command = _decorate_verification("svn update")
    command = "cd '{0}'; {1}".format(svn_dir_path, command)
    return _run_command(command, print_log=print_log)


def commit(svn_dir_path, message="default message", print_log=True):
    command = "svn commit -m '{0}'".format(message)
    command = _decorate_verification(command)
    command = "cd '{0}'; {1}".format(svn_dir_path, command)
    return _run_command(command, print_log=print_log)


def revert_dir(svn_dir_path, print_log=True):
    command = "svn revert -R '{}'".format(svn_dir_path)
    return _run_command(command, print_log=print_log)


def status(svn_dir_path, print_log=True):
    command = "cd '{0}'; svn status".format(svn_dir_path)
    return _run_command(command, print_log=print_log)


def work_space_is_clean(svn_dir_path):
    out_put = status(svn_dir_path)[1]
    if (out_put is None) or (len(out_put) == 0):
        return True
    else:
        return False


def auto_analyse_add(svn_dir_path, print_log=True):
    command = """cd %s; svn status | \
    awk  '/^\?/{ final=substr($0, 2, length()) ;if(index($0,"@") > 0){final=final"@"} print final }' | \
    xargs  -I{} svn add '{}' """ % svn_dir_path
    return _run_command(command, print_log=print_log)


def auto_analyse_delete(svn_dir_path, print_log=True):
    command = """cd %s; svn status | \
    awk  '/^\!/{ final=substr($0, 2, length()) ;if(index($0,"@") > 0){final=final"@"} print final }' | \
    xargs  -I{} svn delete '{}' """ % svn_dir_path
    return _run_command(command, print_log=print_log)
